<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="inc/default :: default"></head>
<body style="background-color: #fff">
<form class="layui-form layer-pop" id="editForm" name="editForm" style="margin-left:10px;" th:object="${systemRole}">
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">角色名：</label>
        <div class="layui-input-block">
            <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" class="layui-input" onblur="checkExist();" th:value="*{name}"/>
        </div>
        <font style="color:red;float: right;margin-top: -30px;">*</font>
    </div>
    <div class="layui-form-item layui-col-xs6">
        <label class="layui-form-label">备注：</label>
        <div class="layui-input-block">
            <input type="text" name="note" id="note" lay-verify="required" autocomplete="off" class="layui-input" th:value="*{note}">
        </div>
        <font style="color:red;float: right;margin-top: -30px;">*</font>
    </div>
    <input type="hidden" name="roleId" id="roleId" th:value="*{roleId}">
    <button type="submit" class="layui-btn" lay-submit lay-filter="submit1" style="display:none;"></button>
</form>
</body>
<script type="text/javascript" th:inline="javascript">
    /*<![CDATA[*/
    var ctxPath = /*[[@{/}]]*/ '';
    /*]]>*/
    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引

    ;!function(){
        var layer1 = layui.layer
            ,form = layui.form;

        form.on('submit(submit1)', function(data){
            var type = [[${type}]];
            var msg = "新增";
            if ("update" == type) {
                msg = "修改";
            }
            $.ajax({
                type: 'post',
                url : ctxPath + 'systemRole/' + [[${type}]],
                data : data.field,
                success : function(result) {
                    if (result) {
                        layer1.msg(msg + "角色成功！", {
                            icon : 1,
                            time : 2000
                        }, function() {
                            if (index) {
                                parent.layer.close(index);
                            }
                            parent.tableIns.reload();
                        });
                    } else {
                        layer1.msg(msg + "角色失败！", {
                            icon : 2,
                            anim : 6,
                            time : 2000
                        });
                    }
                },
                error : function() {
                    layer1.msg(msg + "角色失败！", {
                        icon : 2,
                        anim : 6,
                        time : 2000
                    });
                }
            });
            return false;
        });
    }();

    //检查账号是否存在
    function checkExist() {
        if ($('#name').val() != [[${systemRole.name}]]) {
            $.post(ctxPath + "systemRole/checkExist",
                {"name": $('#name').val()},
                function (result) {
                    if (result) {
                        layer.msg("角色" + $('#name').val() + "已存在！", {
                            icon: 2,
                            anim: 6,
                            time: 2000
                        });
                        $('#name').val('');
                    }
                }
            );
        }
    }

</script>